﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace BLL.Models
{
    public class MShipMModels
    {
        public string[] str_files = { "ID", "Name", "Price", "Intro", "Flag" };
        /// <summary>
        /// 添加配送方式
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool AddShip(MODEL.MEms model)
        {

            bool ret = false;
            string str_sql = "insert into [tb_mc_Ems] ( Name, Price, Intro, Flag)values( '" + model.Name + "'," + model.Price.ToString() + " ,'" + model.Intro + "' ," +model.Flag.ToString() + " )";
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            try
            {
                db.ExecuteNonQuery(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql);
                ret = true;
            }
            catch (Exception ex) { }
            return ret;
        }
        /// <summary>
        /// 修改配送方式
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool UpdateShip(MODEL.MEms model)
        {

            bool ret = false;
            string str_sql = "update [tb_mc_ems] set  Name='"+model.Name+"', Price="+model.Price.ToString()+", Intro='"+model.Intro+"', Flag="+model.Flag.ToString()+" where id= " + model.Id.ToString();

            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            try
            {
                db.ExecuteNonQuery(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql);
                ret = true;
            }
            catch (Exception ex) { }
            return ret;
        }
        /// <summary>
        /// 删除配送方式
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DelShip(string id)
        {

            bool ret = false;
            string str_sql = " delete from [tb_mc_ems] where id in(" + id + ") ";
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            try
            {
                db.ExecuteNonQuery(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql);
                ret = true;
            }
            catch (Exception ex) { }
            return ret;
        }
        /// <summary>
        /// 查询配送方式分页
        /// </summary>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <param name="fiter"></param>
        /// <returns></returns>
        public string AllShip(string start, string limit, string fiter)
        {

            string ret = string.Empty;
            string str_sql = "select top " + limit + " * from [tb_mc_ems] where id not in( select " + start + " id from [tb_mc_ems] order by id desc ) order by id desc";
            if (start == "0")
            {

                str_sql = " select top " + limit + " * from [tb_mc_ems] order by id desc ";
            }
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            DataTable dt = new DataTable();
            string tcount = "0";
            try
            {
                dt = db.ExecuteDataset(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql).Tables[0];
                tcount = db.ExecuteScalar(COMM.PucConfig.ConnectionStringA, CommandType.Text, "select count(*) from [tb_mc_ems] ").ToString();
            }
            catch (Exception ex) { }

            BLL.JSONHelper jsons = new BLL.JSONHelper();



            foreach (DataRow dr in dt.Rows)
            {

                foreach (string s in str_files)
                {

                    jsons.AddItem(s, dr[s].ToString());
                }
                jsons.ItemOk();

            }


            jsons.totlalCount = Convert.ToInt32(tcount);
            ret = jsons.ToString();
            return ret;
        }
       /// <summary>
       /// 查询一条配送方式
       /// </summary>
       /// <param name="id"></param>
       /// <returns></returns>
        public string GetShipById(string id)
        {

            string ret = string.Empty;
            string str_sql = "select * from [tb_mc_ems] where id=" + id;
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            DataTable dt = new DataTable();
            try
            {
                dt = db.ExecuteDataset(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql).Tables[0];
            }
            catch (Exception ex) { }
            BLL.FormJSONHelper jsons = new BLL.FormJSONHelper();
            jsons.success = true;


            foreach (DataRow dr in dt.Rows)
            {

                foreach (string s in str_files)
                {

                    jsons.AddItem(s, dr[s].ToString());
                }
                jsons.ItemOk();

            }



            ret = jsons.ToString();
            return ret;
        }
        public DataTable GetShipByShip(string fiter) {

            DataTable dt = new DataTable();

            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            string str_sql = "  select  * from [tb_mc_ems] where " + fiter;
            try
            {

                dt = db.ExecuteDataset(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql).Tables[0];
            }
            catch (Exception ex) { }

            return dt;
        }
        /// <summary>
        /// 获得快递名称
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetShipNameByid(string id) {

            string ret = string.Empty;
            DataTable dt = new DataTable();
            dt = GetShipByShip(" id=" + id);
            ret = dt.Rows[0]["name"].ToString();
            return ret;
        }
        public string GetShipPriceByid(string id) {

            string ret = string.Empty;
            DataTable dt = new DataTable();
            dt = GetShipByShip(" id=" + id);
            ret = dt.Rows[0]["price"].ToString();
            return ret;
        }
    }
}
